﻿﻿@model WebApp.Models.Company
<!-- 弹出窗体form表单 -->
  <div id="companydetailwindow" class="easyui-window"
       title="明细数据"
       data-options="modal:true,
                    closed:true,
                    minimizable:false,
                    collapsible:false,
                    maximized:false,
                    iconCls:'icon-more',
                    footer:'#windowfooter',
                    closeDuration:200,
                    closeAnimation:'slide',
                    openDuration:300,
                    openAnimation:'slide',
                    onBeforeClose:function(){
                      var that = $(this);
                      if(companyhasmodified()){
                        $.messager.confirm('确认','你确定要放弃保存修改的记录?',function(r){
                        if (r){
                          var opts = that.panel('options');
                          var onBeforeClose = opts.onBeforeClose;
                          opts.onBeforeClose = function(){};
                          that.panel('close');
                          opts.onBeforeClose = onBeforeClose;
                          hook = false;
                        }
                        });
                        return false;
                      }
                    },
                    onOpen:function(){
                       $(this).window('vcenter');
                       $(this).window('hcenter');
                    },
                    onRestore:function(){
                    },
                    onMaximize:function(){
                    }
                    " style="width:820px;height:420px;display:none">
    <!-- toolbar -->
    <div class="panel-content py-2 rounded-bottom border-faded border-left-0 border-right-0  text-muted">
      <div class="row m-0">
        <div class="col-sm-8">
          <div class="btn-group btn-group-sm">
            <button name="saveitembutton" onclick="savecompanyitem()" class="btn btn-default">
              <i class="fa fa-floppy-o"></i> @Html.L("Save")
            </button>
          </div>
          <div class="btn-group btn-group-sm" id="deleteitem-btn-group">
            <button onclick="deletecompanyitem()" class="btn btn-danger">
              <i class="fa fa-trash-o"></i> @Html.L("Delete")
            </button>
          </div>
          <div class="btn-group btn-group-sm" style="display:none">
            <button onclick="todo()" class="btn btn-default">
              <i class="fa fa-print"></i> ToDo
            </button>
          </div>
        </div>
        <div class="col-sm-4 text-align-right">

        </div>
      </div>
    </div>
    <!-- panel form -->
    <div class="panel-content">
      <form id="company_form"
            class="easyui-form form-horizontal p-1"
            method="post"
            data-options="novalidate:true,
                            onChange: function(target){
                              hook = true;
                              $('button[name*=\'saveitembutton\']').prop('disabled', false);
                             },
                             onLoadSuccess:function(data){
                               hook = false;
                               $('button[name*=\'saveitembutton\']').prop('disabled', true);
                             }">
        @Html.AntiForgeryToken()
        <!--Primary Key-->
        @Html.HiddenFor(model => model.Id)
        <fieldset class="form-group">
          <!-- begin row -->
          <!--名称-->
          <div class="row m-1">
            <label class="col-md-2 form-label text-right text-danger">@Html.DisplayNameFor(model => model.Name)</label>
            <div class="col-md-4 mb-1">
              <input id="@Html.IdFor(model => model.Name)"
                     name="@Html.NameFor(model => model.Name)"
                     value="@Html.ValueFor(model => model.Name)"
                     tabindex="0" required
                     class="easyui-textbox"
                     style="width:100%"
                     type="text"
                     data-options="prompt:'@Html.DisplayNameFor(model => model.Name)',
								 required:true,
                                 validType: 'length[0,50]'
                                 " />
            </div>
            <label class="col-md-2 form-label text-right text-danger">@Html.DisplayNameFor(model => model.Code)</label>
            <div class="col-md-4 mb-1">
              <input id="@Html.IdFor(model => model.Code)"
                     name="@Html.NameFor(model => model.Code)"
                     value="@Html.ValueFor(model => model.Code)"
                     tabindex="1" required
                     class="easyui-textbox"
                     style="width:100%"
                     type="text"
                     data-options="prompt:'@Html.DisplayNameFor(model => model.Code)',
								 required:true,
                                 validType: 'length[0,12]'
                                 " />
            </div>
            <label class="col-md-2 form-label text-right">@Html.DisplayNameFor(model => model.Address)</label>
            <div class="col-md-4 mb-1">
              <input id="@Html.IdFor(model => model.Address)"
                     name="@Html.NameFor(model => model.Address)"
                     value="@Html.ValueFor(model => model.Address)"
                     tabindex="2"
                     class="easyui-textbox"
                     style="width:100%"
                     type="text"
                     data-options="prompt:'@Html.DisplayNameFor(model => model.Address)',
								 required:false,
                                 validType: 'length[0,50]'
                                 " />
            </div>
            <label class="col-md-2 form-label text-right">@Html.DisplayNameFor(model => model.Contect)</label>
            <div class="col-md-4 mb-1">
              <input id="@Html.IdFor(model => model.Contect)"
                     name="@Html.NameFor(model => model.Contect)"
                     value="@Html.ValueFor(model => model.Contect)"
                     tabindex="3"
                     class="easyui-textbox"
                     style="width:100%"
                     type="text"
                     data-options="prompt:'@Html.DisplayNameFor(model => model.Contect)',
								 required:false,
                                 validType: 'length[0,12]'
                                 " />
            </div>
            <label class="col-md-2 form-label text-right">@Html.DisplayNameFor(model => model.PhoneNumber)</label>
            <div class="col-md-10 mb-1">
              <input id="@Html.IdFor(model => model.PhoneNumber)"
                     name="@Html.NameFor(model => model.PhoneNumber)"
                     value="@Html.ValueFor(model => model.PhoneNumber)"
                     tabindex="4"
                     class="easyui-textbox"
                     style="width:100%"
                     type="text"
                     data-options="prompt:'@Html.DisplayNameFor(model => model.PhoneNumber)',
								 required:false,
                                 validType: 'length[0,20]'
                                 " />
            </div>
            <label class="col-md-2 form-label text-right text-danger">@Html.DisplayNameFor(model => model.RegisterDate)</label>
            <div class="col-md-4 mb-1">
              <input id="@Html.IdFor(model => model.RegisterDate)"
                     name="@Html.NameFor(model => model.RegisterDate)"
                     value="@Html.ValueFor(model => model.RegisterDate)"
                     tabindex="5" required
                     class="easyui-datebox"
                     style="width:100%"
                     type="text"
                     data-options="prompt:'@Html.DisplayNameFor(model => model.RegisterDate)',
								 required:true,
								 formatter:dateformatter" />
            </div>
          </div>
        </fieldset>
      </form>

      <!--end form表单界面-->
      <!--========================================================================================-->
    </div>
    <!--end window body -->
  </div>
<div id="windowfooter" style="padding:5px;">&nbsp;</div>
<!-- 弹出窗体form表单 -->
<!-- 弹出编辑对话框 -->
<!--begin popup detailview javascript block -->
<script type="text/javascript">
  //判断新增编辑状态
  var MODELSTATE = 'Added';
  var companyid = null;
  function opencompanydetailwindow(data, state) {
    MODELSTATE = state;
    initcompanydetailview();
    companyid = (data.Id || 0);
    $("#companydetailwindow").window("open");
    $('#company_form').form('reset');
    $('#company_form').form('load', data);
  }
  //删除当前记录
  function deletecompanyitem() {
    $.messager.confirm('确认', '你确定要删除该记录?', result => {
      if (result) {
        const url = `/Companies/Delete/${companyid}`;
        $.get(url).done(res => {
          if (res.success) {
            toastr.success("删除成功");
            $dg.datagrid('reload');
            $("#detailswindow").window("close");
          } else {
            $.messager.alert("错误", res.err, "error");
          }
        });
      }
    });
  }
  //async 保存数据
  async function savecompanyitem() {
    const $companyform = $('#company_form');
    if ($companyform.form('enableValidation').form('validate')) {
      let company = $companyform.serializeJSON();
      let url = '/Companies/Edit';
      //判断是新增或是修改方法
      if (MODELSTATE === 'Added') {
        url = '/Companies/Create';
      }
      var token = $('input[name="__RequestVerificationToken"]', $companyform).val();
      $.messager.progress({ title: '请等待', msg: '正在保存数据...', interval: 200 });
      $.ajax({
        type: "POST",
        url: url,
        data: {
          __RequestVerificationToken: token,
          company: company
        },
        dataType: 'json',
        contentType: 'application/x-www-form-urlencoded; charset=utf-8'
      })
        .done(response => {
          $.messager.progress('close');
          if (response.success) {
            hook = false;
            $companyform.form('disableValidation');
            $dg.datagrid('reload');
            $('#companydetailwindow').window("close");
            toastr.success("保存成功");
          } else {
            $.messager.alert("错误", response.err, "error");
          }
        })
        .fail((jqXHR, textStatus, errorThrown) => {
          $.messager.progress('close');
          $.messager.alert('异常', `${jqXHR.status}: ${jqXHR.statusText} `, 'error');
        });
    }
  }
  //关闭窗口
  function closecompanydetailwindow() {
    $('#companydetailwindow').window('close');
  }
  //todo
  function todo() {
    console.log('todo.....');
  }
  //判断是否有没有保存的记录
  function companyhasmodified() {
    return hook;

  }


  function initcompanydetailview() {
    //判断是否显示功能按钮
    if (MODELSTATE === 'Added') {
      $('#deleteitem-btn-group').hide();
    } else {
      $('#deleteitem-btn-group').show();
    }

    //回车光标移动到下个输入控件
    //日期类型 注册日期
    $('#RegisterDate').datebox('textbox').bind('keydown', function (e) {
      if (e.keyCode == 13) {
        $(e.target).emulateTab();
      }
    });
  }
</script>

